package com.app.mingluxing.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.app.mingluxing.R;
import com.app.mingluxing.babytree.ReleaseActivity;
import com.app.mingluxing.bean.BitmapDetail;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

import java.util.List;


/**
 * Created by Aaron on 2015/7/22.
 */
public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    private List<BitmapDetail> bitmapsDetail;
    private List<BitmapDetail> loadedPics;
    private List<String> pics;

    public ImageAdapter(Context ctx, List<BitmapDetail> bitmapsDetail,List<String> pics) {
        this.ctx = ctx;
        this.bitmapsDetail = bitmapsDetail;
        this.pics = pics;
    }

    public void setBitmapsDetail(List<BitmapDetail> bitmapsDetail,List<String> pics) {
        this.bitmapsDetail = bitmapsDetail;
        this.pics = pics;
    }

    @Override
    public int getCount() {
        return bitmapsDetail.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_image, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.iv_delete.setVisibility(View.INVISIBLE);

        if (Constant.ADDBITMAPPATH.equals(bitmapsDetail.get(position).mPath)) {

            viewHolder.iv_image.setImageResource(R.drawable.icon_add);
            viewHolder.iv_delete.setVisibility(View.INVISIBLE);

        } else {
            viewHolder.iv_image.setImageBitmap(CommonUtils.getBitmapFromPath(bitmapsDetail.get(position).mPath));
            viewHolder.iv_delete.setVisibility(View.VISIBLE);
        }

        viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除照片
                String url = bitmapsDetail.get(position).mPath;
                bitmapsDetail.remove(position);
                pics.remove(url);
//                LogUtil.e("tag", "myadapter集合的个数前:" + MyAdapter.mSelectedImage.size());


                //照片选择的页面 集合中的图片也删除掉
                MyAdapter.mSelectedImage.remove(url);

//                LogUtil.e("tag", "myadapter集合的个数后:" + MyAdapter.mSelectedImage.size());
//                EventBus.getDefault().post(true);

//                MyAdapter.
                //编辑页面的 图片集合 也删除掉
//                if (ctx instanceof EditMessageActivity) {
//
//                    EditMessageActivity editMessageActivity = (EditMessageActivity) ctx;
//                    List<BitmapDetail> loadedPics = editMessageActivity.loadedPics;
//                    loadedPics.remove(position);
//                } else
                if (ctx instanceof ReleaseActivity) {

                    ReleaseActivity editMessageActivity = (ReleaseActivity) ctx;
                    loadedPics = editMessageActivity.loadedPics;
                    loadedPics.remove(position);

                }

//                LogUtil.e("TAG", "移除的图片的地址==>" + "adapter中的::::" + MyAdapter.mSelectedImage.get(position)
//                        + "activity中的======>" + loadedPics.get(position).mPath);

                if (loadedPics.size() == 8) {
//                    LogUtil.e("tag", "bi?tmapsDetail个数:" + MyAdapter.mSelectedImage.size());
                    bitmapsDetail.add(new BitmapDetail(Constant.ADDBITMAPPATH, Constant.ADDBITMAPPATH));
                }

                notifyDataSetChanged();
            }
        });


        return convertView;
    }

    private class ViewHolder {
        private ImageView iv_image;
        private ImageView iv_delete;
    }


}
